package ModelLayer;
import java.util.ArrayList;

/**
 * Write a description of class DvdContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class DvdContainer
{
    // instance variables - replace the example below with your own
    private ArrayList<Dvd> dvdList;
    private static DvdContainer instance;

    /**
     * Constructor for objects of class DvdContainer
     */
    private DvdContainer()
    {
        dvdList = new ArrayList<Dvd>();
    }
    
    //Returns a static instance of DvdContainer
    public static DvdContainer getInstance()
    {
        if(instance == null)
        {
            instance = new DvdContainer();
        }

        return instance;
    }

    //Adds a Dvd to the dvdList
    public void addDvd(Dvd newDvd)
    {
        dvdList.add(newDvd);
    }

    //Searches the dvdList by id and runs the getInfo() method on Dvd when found
    public void getDvd(long id)
    {
        boolean found = false;
        int index = 0;
        while(index < dvdList.size() && found == false)
        {
            String id1 = String.valueOf(id);
            String idIndex = String.valueOf(dvdList.get(index).getId());

            if(idIndex.equals(id1))
            {
                dvdList.get(index).getInfo();
                found = true;
            }
            else
            {
                index++;
            }
        }
        
            
            if(found==false)
            {
                System.out.println("You don't have a dvd with that ID... maybe you should check the dvd-list?");
            }
    }
    
    public void getDvdInfo(long id)
    {
        boolean found = false;
        int index = 0;
        while(index < dvdList.size() && found == false)
        {
            String id1 = String.valueOf(id);
            String idIndex = String.valueOf(dvdList.get(index).getId());

            if(idIndex.equals(id1))
            {
                dvdList.get(index).getInfoOnlyDvd();
                found = true;
            }
            else
            {
                index++;
            }
        }
    }

    //Search and delete
    public void deleteDvd(long id)
    {
        int index = 0;
        boolean found = false;

        while(index < dvdList.size() && found == false)
        {

            String id1 = String.valueOf(id);
            String idIndex = String.valueOf(dvdList.get(index).getId());

            if(idIndex.equals(id1))
            {
                dvdList.remove(index);
                System.out.println("Dvd has been deleted!");
                found = true;
            }
            else
            {
                index++;
            }
        }
        
            
            if(found==false)
            {
                System.out.println("You don't have a dvd with that ID... maybe you should check the dvd-list?");
            }
    }

    //Search and update
    public void updateDvd(long id, String title, String author, String releaseDate)
    {
        int index = 0;
        boolean found = false;

        while(index < dvdList.size() && found == false)
        {
            String id1 = String.valueOf(id);
            String idIndex = String.valueOf(dvdList.get(index).getId());

            if(idIndex.equals(id1))
            {
                dvdList.get(index).changeInfo(id, title, author, releaseDate);
                found = true;
                System.out.println("Dvd has been updated!");
            }
            else
            {
                index++;
            }
        }
        
            
            if(found==false)
            {
                System.out.println("You don't have a dvd with that ID... maybe you should check the dvd-list?");
            }
    }

    //Search and adds a sample to the sampleList on the specific Dvd
    public void addSample(long id, long serialNo, String acqDate, double acqPrice)
    {
        int index = 0;
        boolean found = false;

        while(index < dvdList.size() && found == false)
        {

            String id1 = String.valueOf(id);
            String idIndex = String.valueOf(dvdList.get(index).getId());

            if(idIndex.equals(id1))
            {
                found = true;

                dvdList.get(index).addSample(serialNo, acqDate, acqPrice);               
            }
            else
            {
                index++;
            }
        }
        

            if(found==false)
            {
                System.out.println("You don't have a dvd with that ID to add a sample to... maybe you should make a new dvd or check the dvd-list?");
            }
    }

    //Searches and calls the getSample on the object
    public void getSample(long id, long serialNo)
    {
        //this method is to search the container for a person
        //with an id-number equal to the parameter id
        //when person is found, the method is to call print-methods in the TUI layer
        boolean found = false;
        int index = 0;
        while(index < dvdList.size() && found == false)
        {
            String id1 = String.valueOf(id);
            String idIndex = String.valueOf(dvdList.get(index).getId());

            if(idIndex.equals(id1))
            {
                dvdList.get(index).getSample(serialNo);
                found = true;
            }
            else
            {
                index++;
            }
        }
        

            if(found==false)
            {
                System.out.println("You don't have a sample with that ID ... maybe you should check your dvd-list?");
            }
    }

    //Search and delete
    public void deleteSample(long id, long serialNo)
    {
        //this method is to search the container for a person
        //with an id-number equal to the parameter id
        //when person is found, the method is to call print-methods in the TUI layer
        boolean found = false;
        int index = 0;
        while(index < dvdList.size() && found == false)
        {
            String id1 = String.valueOf(id);
            String idIndex = String.valueOf(dvdList.get(index).getId());

            if(idIndex.equals(id1))
            {
                dvdList.get(index).deleteSample(serialNo);
                found = true;
            }
            else
            {
                index++;
            }
        }
    }

    //Search and update
    public void updateSample(long id, long serialNo, String acqDate, double acqPrice)
    {
        int index = 0;
        boolean found = false;

        while(index < dvdList.size() && found == false)
        {
            String id1 = String.valueOf(id);
            String idIndex = String.valueOf(dvdList.get(index).getId());

            if(idIndex.equals(id1))
            {
                dvdList.get(index).updateSample(serialNo, acqDate, acqPrice);
                found = true;
                System.out.println("Perso has been updated!");
            }
            else
            {
                index++;
            }
        }
    }

    //Goes thorugh the entire dvdList and calls the getInfo() method for each object
    public void showDvds()
    {
        int index = 0;
        while(index < dvdList.size())
        {
            System.out.println("------");
            System.out.println("Dvd's: ");
            dvdList.get(index).getInfo();
            index++;
        }
        System.out.println("------");
    }
}